package com.ss.android.usergrowth;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.text.TextUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public final class a {
    private static final String a;
    private static final String b;
    private static String c;
    private static String d;
    private static String e;
    private static int f;
    private static final List g;
    private static final List h;
    private static final List i;

    static {
        a = Environment.getRootDirectory().getAbsolutePath();
        b = Environment.getDataDirectory().getAbsolutePath();
        c = "meta_umeng_channel";
        d = "ss.properties";
        e = "com.ss.android.article.news";
        f = 470;
        g = Arrays.asList(new File[]{new File("/system/preload"), new File("/system/delapp"), new File("/data/miui/app"), new File("/cust"), new File("/system/presetapp"), new File("/system/preset_apps"), new File("/system/reserve"), new File("/system/pre-install"), new File("/data/preload"), new File("/system/appbackup"), new File("/system/etc/customization/applications"), new File("/system/etc/property/app"), new File("/apps")});
        h = Collections.singletonList(new File(b, "gn_apps.zip"));
        i = Arrays.asList(new String[]{a + "/app", a + "/framework", "/sys", "/proc", "/etc", "/dev", "/res", "/d", "mnt/sdcard", "/sdcard"});
    }

    public static String a(Context arg7) {
        String v0_5;
        ApkUtils v2_1;
        File v2;
        String v1 = null;
        try {
            LinkedHashSet v3 = new LinkedHashSet();
            PackageManager v0_1 = arg7.getPackageManager();
            a(v3);
            Iterator<ApplicationInfo> v4 = v0_1.getInstalledApplications(0).iterator();
            ApplicationInfo v0_2;
            do {
                label_8:
                if (!v4.hasNext()) {
                    b(arg7, ((Set) v3));
                    ApkUtils v0_4 = a(arg7, ((Set) v3));
                    if (v0_4 != null) {
                        v2_1 = v0_4;
                    } else {
                        ((Set) v3).addAll(i);
                        v2_1 = a(arg7, Environment.getRootDirectory().getParentFile(), ((Set) v3));
                    }

                    if (v2_1 == null) {
                        v0_5 = v1;
                        return v0_5;
                    }

                    if (v2_1 == null) {
                        v0_5 = v1;
                    } else if (v2_1.b < f) {
                        v0_5 = v2_1.a().getText(v2_1.a().getIdentifier(c, "string", v2_1.a)).toString();
                    } else {
                        v0_5 = a(v2_1);
                    }

                    v2_1.c.close();
                    if (arg7 == null) {
                        return v0_5;
                    }

                    if (arg7.getFilesDir() == null) {
                        return v0_5;
                    }

                    v2 = new File(arg7.getFilesDir(), "apks");
                    if (!v2.isDirectory()) {
                        return v0_5;
                    }
                    v2.delete();
                    return v0_5;
                }

                v0_2 = v4.next();
                v2 = new File(((ApplicationInfo) v0_2).sourceDir);
                if (!((ApplicationInfo) v0_2).sourceDir.startsWith(a)) {
                    continue;
                }

                if (a(((ApplicationInfo) v0_2).sourceDir, i)) {
                    continue;
                }

                if (a(((ApplicationInfo) v0_2).sourceDir, ((Collection) v3))) {
                    continue;
                }
                File v0_3 = v2;
                while (!a.equals(v0_3.getParent())) {
                    v0_3 = v0_3.getParentFile();
                }
                ((Set) v3).add(v0_3.getAbsolutePath());
                continue;
            } while (true);
        } catch (Throwable v0) {
            return v1;
        }
    }

    private static ApkUtils a(Context arg6, File arg7, Set arg8) {
        ApkUtils v1_1;
        ApkUtils v0 = null;
        if (arg6 != null && arg7 != null && !a(arg7.getAbsolutePath(), ((Collection) arg8)) && (!arg7.canRead() || !arg7.canWrite() || arg7.getName() == "apks")) {
            if ((arg7.isFile()) && (arg7.getName().endsWith(".apk")) && (arg7.canRead())) {
                try {
                    v1_1 = new ApkUtils(arg7);
                } catch (ApkUtils.ApkUtilsException v1) {
                    v1_1 = v0;
                }

                if (v1_1 == null) {
                    return v0;
                }

                if (e.endsWith(v1_1.a)) {
                    return v1_1;
                }

                v1_1.c.close();
            }

            if (arg7.isDirectory()) {
                File[] v3 = arg7.listFiles();
                if (v3 == null) {
                    return v0;
                }

                int v4 = v3.length;
                int v2;
                for (v2 = 0; v2 < v4; ++v2) {
                    File v1_2 = v3[v2];
                    if (v1_2 == null || !a(arg7)) {
                        v1_1 = a(arg6, v1_2, arg8);
                        if (v1_1 != null) {
                            return v1_1;
                        }
                    }
                }
            }
        }

        return v0;
    }

    private static boolean a(String arg5, Collection<String> arg6) {
        boolean v0;
        if (TextUtils.isEmpty(arg5)) {
            return false;
        } else if (arg6 == null) {
            return false;
        } else {
            Iterator<String> iterator = arg6.iterator();
            do {
                if (iterator.hasNext()) {
                    String v0_1 = iterator.next();
                    if (arg5.equals(v0_1)) {
                        return true;
                    } else if (!arg5.startsWith(v0_1 + File.separator)) {
                        continue;
                    } else {
                        return true;
                    }
                } else {
                    return false;
                }
            }
            while (true);
        }
    }

    private static boolean a(File file) {
        try {
            if (file.getParent() != null) {
                file = new File(file.getParentFile().getCanonicalFile(), file.getName());
            }

            if (file.getCanonicalFile().equals(file.getAbsoluteFile())) {
                return false;
            }
        } catch (IOException ioException) {
            return false;
        }
        return true;
    }

    private static ApkUtils a(Context arg4, Set<String> arg5) throws ApkUtils.ApkUtilsException {
        ApkUtils v0;
        Set v1 = null;
        if (arg4 == null) {
            v0 = null;
        } else {
            Iterator<String> v2 = arg5.iterator();
            do {
                if (v2.hasNext()) {
                    v0 = a(arg4, new File(v2.next()), v1);
                    if (v0 == null) {
                        continue;
                    }
                    return v0;
                } else {
                    return null;
                }
            }
            while (true);
        }

        return v0;
    }

    private static void a(Set arg3) {
        Iterator v1 = g.iterator();
        while (v1.hasNext()) {
            Object v0 = v1.next();
            if (!((File) v0).isDirectory()) {
                continue;
            }

            if (!((File) v0).canRead()) {
                continue;
            }

            arg3.add(((File) v0).getAbsolutePath());
        }
    }

    private static String a(ApkUtils arg3) throws ApkUtils.ApkUtilsException {
        try {
            Properties v0_1 = new Properties();
            InputStream v1 = arg3.c.open(d);
            v0_1.load(v1);
            v1.close();
            return v0_1.getProperty(c);
        } catch (IOException v0) {
            throw new ApkUtils.ApkUtilsException((v0));
        }
    }

    private static void a(File arg6, File arg7) throws IOException {
        int v1;
        String v4;
        Object v0_2;
        ZipFile v2 = new ZipFile(arg7);
        try {
            Enumeration v3 = v2.entries();
            while (true) {
                label_3:
                if (!v3.hasMoreElements()) {
                    return;
                }

                v0_2 = v3.nextElement();
                v4 = ((ZipEntry) v0_2).getName();
                if (((ZipEntry) v0_2).isDirectory()) {
                    continue;
                }
                if (v4 == null) {
                    v1 = 0;
                } else if (!v4.contains(".." + File.separator)) {
                    v1 = 1;
                } else {
                    v1 = 0;
                }

                if (v1 == 0) {
                    continue;
                }

                if (!v4.endsWith(".apk")) {
                    continue;
                }

                InputStream v0_3 = v2.getInputStream(((ZipEntry) v0_2));
                FileOutputStream v4_1 = new FileOutputStream(new File(arg6, v4));
                try {
                    a(v0_3, v4_1);
                } catch (IOException v0_4) {
                }
                continue;
            }
        } catch (Exception v0) {
            v0.printStackTrace();
        } finally {
            v2.close();
            return;
        }
    }

    private static void a(InputStream arg6, OutputStream arg7) throws IOException {
        ZipInputStream v0 = new ZipInputStream(arg6);
        ZipOutputStream v1 = new ZipOutputStream(arg7);
        while (true) {
            ZipEntry v2 = v0.getNextEntry();
            if (v2 == null) {
                break;
            }

            if (v2.isDirectory()) {
                continue;
            }

            String v3 = v2.getName();
            if (!"AndroidManifest.xml".equals(v3) && !"resources.arsc".equals(v3) && !("assets/" + d).equals(v3)) {
                b(((InputStream) v0), null);
                continue;
            }

            v1.putNextEntry(v2);
            b(((InputStream) v0), ((OutputStream) v1));
            v1.closeEntry();
        }

        v1.close();
    }

    private static void b(Context arg5, Set arg6) {
        if (arg5 != null && arg5.getFilesDir() != null) {
            File v1 = new File(arg5.getFilesDir(), "apks");
            if (!v1.exists()) {
                v1.mkdirs();
            } else if (v1.isFile()) {
                return;
            }

            Iterator v2 = h.iterator();
            while (v2.hasNext()) {
                Object v0 = v2.next();
                if (!((File) v0).isFile()) {
                    continue;
                }

                if (!((File) v0).getName().endsWith(".zip")) {
                    continue;
                }

                try {
                    a(v1, ((File) v0));
                } catch (IOException v0_1) {
                }
            }

            arg6.add(v1.getAbsolutePath());
        }
    }

    private static void b(InputStream arg4, OutputStream arg5) throws IOException {
        byte[] v0 = new byte[8192];
        BufferedInputStream v1 = new BufferedInputStream(arg4);
        while (true) {
            int v2 = v1.read(v0);
            if (v2 == -1) {
                return;
            }

            if (arg5 == null) {
                continue;
            }

            arg5.write(v0, 0, v2);
        }
    }
}

